﻿#include "productplantableview.h"
#include <QDebug>
#include <QMessageBox>
#include "readonlydelegate.h"

ProductPlanTableView::ProductPlanTableView(QWidget *parent) : MyTableView(parent)
{
    setSortingEnabled(true);
    model = new ProductPlanStandardItemModel();
    setModel(model);
    setSelectionRow();

    for(int i=0;i<model->columnCount();i++){
        if(i!=10){
            setItemDelegateForColumn(i, new ReadOnlyDelegate());
        }
    }

    connect(this, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        if(index.row() == model->rowCount()-1){
            productPicker.showAndInit();
        }
    });

    connect(&productPicker, &ProductPicker::selectedFinished,
            [=](QList<Product*> products, int defaultCount)
    {
        Q_UNUSED(defaultCount);
        model->insertProduct(products);
    });
}

void ProductPlanTableView::initHiddenState(){
    initHiddenStateList("productPlan", model->header.size());
}

void ProductPlanTableView::setListPushButton(ListPushButton *listPushButton){
    setListPushButtonAndMenuLabel(listPushButton, model->header);
}

void ProductPlanTableView::setSelectionRow(){
    model->appendRow(*(new QList<QStandardItem*>));
    setSpan(model->rowCount()-1, 0, 1, model->columnCount());
}

void ProductPlanTableView::queryPlanProducts(){
    DbHelper helper;

    QList<Product*> products = helper.queryPlanProducts();

    if(products.size() == 0){
        QMessageBox::information(this, "信息", "无符合条件商品！");
    }else{
        model->insertProduct(products);
    }
}

void ProductPlanTableView::clear(){
    model->reset();
    setSelectionRow();

    refreshHiddenState();
}
